<?php
    // estilos -- ver por qué no puedo usar class con un css - otra clase debe estar bloqueandolo
    $tabla_orga_td = 'vertical-align: middle; background-color: transparent; border: 1px;';    

    // imagenes
    $prev_img = $this->Html->image("restaurante/ic_navegar_left.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_left"));    
    $next_img = $this->Html->image("restaurante/ic_navegar.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_right"));    
    $prev_img_disabled = $this->Html->image("restaurante/ic_nav_left_disabled.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_left_disabled"));    
    $next_img_disabled = $this->Html->image("restaurante/ic_navegar_disabled.png", array("width" => "32px", "height" => "32px", "id" => "nav_icon_right_disabled"));    

    $volver_img = $this->Html->image("restaurante/ic_back.png", array("id" => "back_icon"));    
    $nuevo_img = $this->Html->image("restaurante/ic_add.png", array("id" => "add_icon"));    
    $cancel_img = $this->Html->image("restaurante/ic_cancel.png", array("id" => "back_icon"));    
    $save_img = $this->Html->image("restaurante/ic_ok.png", array("id" => "save_icon"));    
    
    $editar_img = $this->Html->image("restaurante/doc_images.png", array("width" => "24px", "height" => "24px"));    
    
?>

<?php $options = array(1 => 'Abierto', 2 => 'Cerrado', 3 => 'Facturado', 4 => 'Cancelado'); ?>    


<?php   App::uses('CakeTime', 'Utility');
        App::uses('CakeNumber', 'Utility');
?>

<!--- MARCO DE LA PANTALLA ------>
<table><tr><td style="padding: 20px; background: white; border: none;">

<table>
    <tr style="border: 2px;">
        <td style="width: 15%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo $this->Html->image("restaurante/ic_resto.png", array("width" => "64px", "height" => "64px", "id" => "resto_icon"));?></td>
        <td style="width: 55%; text-align:left; <?php echo $tabla_orga_td;?>"><h1 class="titulo_ventana">PEDIDOS</h1></td>
        <td style="width: 30%; text-align:right; <?php echo $tabla_orga_td;?>">
            <table>
                <tr><td style="text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Usuario: '.$nombre_usuario;?></td></tr>
                <tr><td style="text-align:right; <?php echo $tabla_orga_td;?>"><?php echo $this->Html->link('Cerrar sesión', array("controller"=>"users", "action"=>"logout"), array('title' => 'Cerrar sesión', 'escape' => false));?></td></tr>
            </table>
        </td>
    </tr>      
</table>
<hr>

<!--- CONTROLES Y BOTONERA FILTROS ------>

<?php echo $this->Form->create('Pedido', array('action'=>'/index','novalidate' => true)); ?>
<table>
    <tr>
        <td style="width: 25%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->input('user_id', array('empty' => '(elija uno)','label' => 'Mozo')); ?></td>
        <td style="width: 15%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->input('filtro_abiertos', array('checked' => false,'label' => 'Abiertos')); ?></td>
        <td style="width: 15%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->input('filtro_facturados', array('checked' => false,'label' => 'Facturados')); ?></td>
        <td style="width: 15%;<?php echo $tabla_orga_td;?>"><?php echo $this->Form->input('filtro_cancelados', array('checked' => false,'label' => 'Cancelados')); ?></td>
        <td style="width: 2%; <?php echo $tabla_orga_td;?>"></td>
        <td style="width: 7%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Buscar'; ?></td>
        <td style="width: 7%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Form->submit('restaurante/ic_search.png',array('class' => 'boton-buscar')); ?> </td>
        <td style="width: 7%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Limpiar'; ?></td>
        <td style="width: 7%; text-align:left; <?php echo $tabla_orga_td;?>"><?php echo $this->Form->button('', array('type'=>'reset','class' => 'boton-limpiar')); ?></td>   
    </tr>
</table>
<?php echo $this->Form->end(); ?>

<?php //Debugger::dump($pedidos); ?>
<!--- GRILLA ------>
<table>
    <tr>
        <th style="width: 10%; text-align: right;"><?php echo $this->Paginator->sort('Pedido.id','Id Pedido',array('direction' => 'asc') ); ?></th>
        <th style="width: 10%;"><?php echo $this->Paginator->sort('Pedido.fecha','Fecha',array('direction' => 'asc') ); ?></th>
        <th style="width: 10%;"><?php echo "Hora"; ?></th>
        <th style="width: 25%;"><?php echo "Mesas Nro."; ?></th>
        <th style="width: 10%; text-align: right;"><?php echo "Importe($)"; ?></th>
        <th style="width: 10%; text-align:center;"><?php echo "Estado"; ?></th>
        <th style="width: 10%; text-align:center;"><?php echo "Detalle"; ?></th>
        <th style="width: 15%;"><?php echo "Atendió"; ?></th>
    </tr>
    <?php foreach ($pedidos as $pedido): ?>
    <tr>
        <td style="width: 10%; text-align: right;"><?php echo $pedido['Pedido']['id']; ?> </td>
        <td style="width: 10%;"><?php echo CakeTime::format($pedido['Pedido']['fecha'], '%d/%m/%y'); ?></td>
        <td style="width: 10%;"><?php echo CakeTime::format($pedido['Pedido']['fecha'], '%R'); ?></td>
        <td style="width: 25%;"><?php echo $pedido[0]['mesas']; ?> </td>
        <td style="width: 10%; text-align: right;"><?php 
            $importe = $pedido[0]['importe_detalles'] + $pedido[0]['importe_agregados'];
            // si no está facturado uso los datos de parametros
            if ($pedido['Pedido']['paga_cubierto'] == 1) {
                $importe += $pedido['Pedido']['precio_uni_cubierto'] *  $pedido['Pedido']['cant_clientes'];
            }
            if (isset($pedido['Descuento']['porcentaje'])){
                $descuento = $importe * $pedido['Descuento']['porcentaje'] / 100;
                $importe -= $descuento;
            }
            echo CakeNumber::precision($importe, 2); ?></td>       
        <td style="width: 10%; text-align:center;"><?php 
            if (isset($pedido['Pedido']['cerrado'])) {
                if ($pedido['Pedido']['facturado'] == 1) {
                    echo $options[3];
                } else {
                    if ($pedido['Pedido']['cancelado'] == 1) { 
                        echo $options[4];    
                    } else {
                        if ($pedido['Pedido']['cerrado'] == 1) { 
                            echo $options[2];
                        } else {
                            echo $options[1];
                        }
                    }
                }
            } else {
                echo $options[1];
            }           
        ?></td>
        <td style="width: 10%; text-align:center;"><?php echo $this->Html->link($editar_img, '/pedidos/detalle/'.$pedido['Pedido']['id'], array('escape' => false)); ?> </td>
        <td style="width: 15%;"><?php 
            if(($pedido['Pedido']['facturado']==1) 
            || ($pedido['Pedido']['cancelado']==1)) {
                echo $pedido['User']['nombre'];
            } else {
                echo $this->Form->input('user_id_'.$pedido['Pedido']['id'], 
                                                array('label' => '', 'options' => $users, 
                                                    'default' => $pedido['User']['id'])); 
            } 
            ?>
        </td>
    </tr>    
    <?php endforeach; ?>    
</table>

<!--- PAGINADO ------>
<table>
    <tr>
        <td style="width: 10%; <?php echo $tabla_orga_td;?>"><?php  echo $this->Paginator->prev(!$this->Paginator->hasPrev() ? null : $prev_img, array('escape' => false), !$this->Paginator->hasPrev() ? $prev_img_disabled : null, array('class'=>'prev disabled','escape' => false));?></td>
        <td style="width: 10%; <?php echo $tabla_orga_td;?>"><?php  echo $this->Paginator->next(!$this->Paginator->hasNext() ? null : $next_img, array('escape' => false), !$this->Paginator->hasNext() ? $next_img_disabled : null, array('class'=>'next disabled','escape' => false));?></td>
        <td style="width: 80%; <?php echo $tabla_orga_td;?>"></td>
    </tr>
</table>

<!--- BOTONERA ------->
<table>
    <tr>
        <td style="width: 10%; text-align:right; <?php echo $tabla_orga_td;?>"><?php echo 'Volver'; ?></td>
        <td style="width: 10%; text-align:left; <?php echo $tabla_orga_td;?>">
            <?php echo $this->Html->link($volver_img, array("controller"=>"mainmenus", "action"=>"index"), array('class' => 'button', 'title' => 'Volver', 'escape' => false)); ?>
        </td>
        <td style="width: 80%; <?php echo $tabla_orga_td;?>"></td>
    </tr>
</table>

<!-- overlayed element -->
<div id="dialogModal">
     <!-- the external content is loaded inside this tag -->
     <div class="contentWrap"></div>
</div>

<!--- FIN MARCO DE LA PANTALLA ------>
</td></tr></table>

<div id="valores"></div>
<?php
    foreach($pedidos as $pedido):
        $this->Js->get('#user_id_'.$pedido['Pedido']['id'])->event('change', 
            $this->Js->request(array(
                'controller'=>'pedidos', 'action'=>'cambiarmozo', $pedido['Pedido']['id']), 
            array(
                'update'=>'#header',
                'async' => true,
                'method' => 'post',
                'data' => $_POST,
                'dataExpression'=>true,
                'data'=> $this->Js->serializeForm(array(
                    'isForm' => true,
                    'inline' => true
                ))
            ))
        );
        
    endforeach;


?>

